home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / portscan.c < prev    next >
C/C++ Source or Header  |  1998-07-17  |  6KB  |  279 lines

  1. /* 
  2.  * internet port scanner 
  3.  *
  4.  * This program will scan a hosts TCP ports printing all ports that accept
  5.  * connections, and if known, the service name.
  6.  * This program can be trivially altered to do UDP ports also.
  7.  *
  8.  * Kopywrong (K) Aug. 25, '94 pluvius@io.org
  9.  *
  10.  * Hey kiddies, this is a C program, to run it do this:
  11.  * $ cc -o pscan pscan.c
  12.  * $ pscan <host> [max port]
  13.  *
  14.  * No, this will not get you root.
  15.  * 
  16.  * Changes:
  17.  * Changed fprintf to printf in line 34 to work with my Linux 1.1.18 box
  18.  * Netrunner 1/18/95 11:30pm
  19.  *
  20.  * Changes:
  21.  * converts port# to network byte order.
  22.  * Therapy 10/29/96 9:00pm
  23.  * 
  24. */
  25. static char sccsid[] = "@(#)pscan.c     1.0     (KRAD) 08/25/94";
  26. #include <stdio.h>
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29. #include <netinet/in.h>
  30. #include <netdb.h>
  31.  
  32. #define MAX_PORT 1024 /* scan up to this port */
  33. int s;
  34. struct sockaddr_in addr;
  35. char rmt_host[100];
  36.  
  37. int skan(port)
  38. int port;
  39. {
  40.  int r;
  41.     s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  42.     if (s < 0) {
  43.        /* fprintf("ERROR: socket() failed\n"); */
  44.        /* Changed to printf for my Linux 1.1.18 box */
  45.        printf("ERROR: socket() failed\n");
  46.        exit(0);
  47.     }
  48.  
  49.     addr.sin_family = PF_INET;
  50.     addr.sin_port = htons(port);
  51.     addr.sin_addr.s_addr = inet_addr(rmt_host);
  52.  
  53.     r = connect(s,(struct sockaddr *) &addr, sizeof(addr));
  54.  
  55.     close(s);
  56.     if (r==-1) {
  57.        return (1 == 0);
  58.     }
  59.  
  60.     return (1 == 1);
  61. }
  62.  
  63. main(argc,argv) 
  64. int argc;
  65. char *argv[];
  66. {
  67.  int a,b,c,d,e,f;
  68.  struct hostent *foo;
  69.  struct servent *bar;
  70.  
  71.    if (argc < 2) {
  72.       fprintf(stderr,"usage: %s <host> [highest port]\n",argv[0]);
  73.       exit(0);
  74.    }
  75.  
  76.    if (sscanf(argv[1],"%d.%d.%d.%d",&a,&b,&c,&d) != 4) {
  77.       foo = gethostbyname(argv[1]);
  78.       if (foo == NULL) {
  79.          fprintf(stderr,"error: cannot resolve host %s\n",argv[1]);
  80.          exit(0);
  81.       }
  82.       sprintf(rmt_host,"%d.%d.%d.%d",(unsigned char )foo->h_addr_list[0][0],
  83.               (unsigned char ) foo->h_addr_list[0][1], 
  84.               (unsigned char ) foo->h_addr_list[0][2], 
  85.               (unsigned char ) foo->h_addr_list[0][3]);
  86.    } else {
  87.       strncpy(rmt_host,argv[1],99);
  88.    }
  89.  
  90.  
  91.    if (argc > 2) {
  92.       f = atoi(argv[2]);
  93.    } else
  94.       f = MAX_PORT;
  95.  
  96.    fprintf(stdout,"Scanning host %s - TCP ports 1 through %d\n",rmt_host,f);
  97.  
  98.    for (e =1;e<=f;e++) {
  99.     char serv[100];
  100.       if (skan(e)) {
  101.          bar = getservbyport(htons(e),"tcp"); 
  102.          printf("%d (%s) is running.\n",e,(bar == NULL) ? "UNKNOWN" :
  103.                 bar->s_name); 
  104.       }
  105.    }
  106. }
  107.  
  108.  
  109.  
  110.  
  111. ------------------------------------------------------------------------------------
  112.  
  113.     Here is another simple portscanner written in PERL
  114.  
  115. #!/usr/bin/perl
  116. #
  117. # A simple TCP port scanner in perl
  118. # James.Abendschan@nau.edu  27 January 1996
  119. #
  120. # output to stdout, logging to stderr
  121. #
  122. # todo - 
  123. #  better arg handling :)
  124. #  fork() scans?
  125.  
  126. # ports to scan:
  127. # 21 - ftp
  128. # 23 - telnet
  129. # 25 - smtp
  130. # 79 - finger
  131. # 80 - www
  132. # 119 - nntp
  133. # 139 - netbios (wfwg over tcpip)
  134. # 8000 - occasional www
  135. # 8080 - ocassional www
  136.  
  137. @myports = (21, 23, 25, 79, 80, 119, 139, 8000, 8080);
  138.  
  139. require 'sys/socket.ph';
  140. require 'flush.pl';
  141.  
  142. $SIG{'ALRM'} = 'do_alarm';
  143.  
  144. if ($ARGV[0] eq "") {
  145.  print "please provide a subnet to scan!  e.g., 134.114.84\n";
  146.  exit 1;
  147. }
  148.  
  149. {
  150.  $net = $ARGV[0];
  151.  ($a, $b, $c) = split(/\./, $net);
  152.  
  153.   for ($d=0;$d<256;$d++) {
  154.    $host = "$a.$b.$c.$d";
  155.  
  156.    @portlist = @myports;
  157.  
  158.    while(@portlist) {
  159.     $port = shift(@portlist);
  160.     print STDERR "Trying $host:$port\n";
  161.     $data = scan($host, $port);
  162.     if (index($data, "FAILED") != 0) {
  163.      @addr = split(/\./, $host);
  164.      $addr = pack(' C4', @addr[0], @addr[1], @addr[2], @addr[3]);
  165.      ($name, $aliases, $type, $len, @addrs) = gethostbyaddr($addr, 2);
  166.      if ($name eq "") {
  167.       $name = $host;
  168.      }
  169.      print STDOUT "$name:$port:$data\n";
  170.      flush(STDOUT);
  171.     }
  172.    }
  173.   }
  174. }
  175.  
  176. #
  177. # scan (host, port)
  178. # returns error or banner
  179. #
  180.  
  181. sub scan
  182. {
  183.  
  184.  $hostname = shift @_;
  185.  $serverport = shift @_;
  186.  
  187.  $connecttimeout = "1";        # time to wait for a reply
  188.  $bannertimeout = "7";        # time to wait for data after a connect
  189.  
  190. init:
  191.     
  192.    # seed & pick a random port number 
  193.  
  194.    for ($i=0; $i < $$; $i++) { rand(); }
  195.  
  196.    $clientport = int(rand(32768) + 1024);
  197.    $sockaddr = 'S n a4 x8';
  198.    $locport=pack($sockaddr, &AF_INET, $clientport, "\0\0\0\0");
  199.  
  200.    if (!socket(C, &PF_INET, &SOCK_STREAM, $proto)) {
  201.     #print "WARNING - couldn't create client socket: $!\n";
  202.     sleep 5;
  203.     goto init;
  204.    }
  205.  
  206.    if (!bind(C, $locport)) { 
  207.     # die("cannot bind client socket: $!\n");
  208.     #print "WARNING - couldn't bind client socket:$!\n";
  209.     sleep (5);
  210.     goto init;
  211.    }
  212.  
  213.    ($name, $aliases, $proto) = getprotobyname('tcp');
  214.    ($name, $aliases, $type, $len, $thisaddr) = gethostbyname($hostname);
  215.    ($a,$b,$c,$d) = unpack('C4', $thisaddr);
  216.  
  217.    $ipaddr="$a.$b.$c.$d";
  218.    
  219.    $thatport = pack($sockaddr, &AF_INET, $serverport, $thisaddr);
  220.    
  221.    alarm(0);
  222.    alarm($connecttimeout);
  223.  
  224.    if (!connect(C, $thatport)) {
  225.     return "FAILED: $!\n";
  226.    }
  227.  
  228.    alarm(0);
  229.  
  230.    select(C); 
  231.    $| = 1;
  232.    select(STDOUT);
  233.  
  234.    # Now send/rec data to C
  235.  
  236.    # nudge it..
  237.  
  238.    print C "\r\n";
  239.  
  240.    alarm($bannertimeout); 
  241.  
  242.    $banner = "";
  243.    while ($data = <C>) {
  244.     $banner = "$banner$data" 
  245.    }
  246.  
  247.    alarm(0);
  248.  
  249.    if ($banner eq "") {
  250.     $banner = $data;
  251.    }
  252.  
  253.    shutdown(C, 1);
  254.    close(C);
  255.  
  256.    $banner =~ tr/\r/\./;
  257.    $banner =~ tr/\n/\./;
  258.  
  259.    return $banner;
  260. }
  261.  
  262. #
  263. # Handle timeouts
  264. #
  265.  
  266. sub do_alarm {
  267.   alarm(0); # reset alarm clock
  268.   $SIG{'ALRM'} = 'do_alarm';
  269.   close (C);
  270.   return "FAILED: timeout";
  271. }
  272.  
  273.  
  274.  
  275.  
  276.  
  277. --------------------------------------------------------------------------------
  278.  
  279.